﻿using System.CodeDom;
using System.ComponentModel;
using Roslyn.Compilers.CSharp;

namespace WeaverCustomTool
{
    public struct TypeDeclaration
    {
        public CodeTypeDeclaration FromInterface(InterfaceDeclarationSyntax @interface)
        {
            var weavedType = new CodeTypeDeclaration(@interface.Identifier.ValueText.TrimStart('I'))
            {
                IsClass = true,
                IsPartial = true
            };
            var weavedInterface = new CodeTypeDeclaration(@interface.Identifier.ValueText)
            {
                IsInterface = true
            };
            weavedType.BaseTypes.Add(new CodeTypeReference(weavedInterface.Name));

            // Check whether need to add INotifyPropertyChanged
            if (@interface.NeedsINotifyPropertyChangedImplementation())
            {
                weavedType.BaseTypes.Add(new CodeTypeReference(typeof(INotifyPropertyChanged)));
                weavedType.Members.Add(new CodeMemberEvent
                    {
                        Name = "PropertyChanged",
                        Type = new CodeTypeReference(typeof (PropertyChangedEventHandler)),
                        Attributes = MemberAttributes.Public
                    });
            }

            return weavedType;
        }
    }
}
